
// 导入路由
import VueRouter from 'vue-router'

// 注册路由
import Vue from 'vue'
Vue.use(VueRouter)

// 创建路由实例
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/HomeView'),
      meta: { title: '首页' }
    },
    {
      path: '/category',
      name: 'category',
      component: () => import('@/views/CategoryView'),
      meta: { title: '分类' }
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('@/views/OrderView'),
      meta: { title: '订单' }
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('@/views/MyView'),
      meta: { title: '我的' }
    },
    {
      path: '*',
      name: 'err404',
      component: () => import('@/views/NotFound'),
      meta: { title: '页面未找到' }
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) document.title = to.meta.title // 页面标题
  next()
})

export default router
